/*
 * RBFCom.c
 *
 *  Created on: 2023年7月12日
 *      Author: fjx
 */
#include "RBFCommon.h"
#include "util.h"


#define xCONFIG_LOG_RBFCOMMON                //模块日志宏定义,使能,模块输出日志

#if defined (CONFIG_LOG_RBFCOMMON)
#include "util/log.h"

void RBF_header_log (RBFMacHeader_t hdr)
{

  printf("rollungCode = 0x%0lx\n",hdr.rollungCode);
  printf("frameCtl = 0x%0x\n",hdr.frameCtl);
  printf("syncClock = 0x%0x\n",hdr.syncClock);
  printf("hsclock = 0x%0x\n",hdr.hsclock);

  printf("panId = 0x%0x\n",hdr.panId);
  printf("srcId = 0x%0x\n",hdr.srcId);
  printf("destID = 0x%0x\n",hdr.destID);
  printf("routID = 0x%0x\n",hdr.routID);
  printf("cmd    = 0x%0x\n",hdr.cmd);

}
#endif


/*
 * nonce: rollungCode(4B) + frameCtl(1B) + panId(3B) + srcId(1B)
 * nonce length max 13, current 9B
 * return
 */
RBF_RET_STATUS RBF_payload_crypto_build_nonce (const uint8_t *packet, uint16_t packet_len,
                                     uint8_t nonce[RBF_NONCE_LENGTH_MAX])
{
  RBFMacHeader_t hdr;
  uint32_t padID_srcID = 0;

  if(packet_len < RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX ){

      return RBF_RET_STATUS_FAIL;
  }

  hdr.rollungCode = l_get_be32(packet+RBF_MAC_HEADER_INDEX_ROLLUNGCODE);
  hdr.frameCtl = l_get_u8(packet+RBF_MAC_HEADER_INDEX_FRAMECTL); //4
  padID_srcID = l_get_be32(packet+RBF_MAC_HEADER_INDEX_PANDID);  //获取panID(bit31~bit8) + srcID(bit7~bit0)

  l_put_be32(hdr.rollungCode,nonce);
  l_put_u8(hdr.frameCtl,nonce+4);
  l_put_be32(padID_srcID,nonce+5);

  return RBF_RET_STATUS_SUCCESS;
}

/*
 * ad: destID(1B) + routID(1B) + cmd(1B)
 *
 */
RBF_RET_STATUS RBF_payload_crypto_build_ad (const uint8_t *packet, uint16_t packet_len,
                                  uint8_t ad[RBF_AD_LENGTH_MAX])
{
  RBFMacHeader_t hdr;

  if(packet_len < RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX ){

      return RBF_RET_STATUS_FAIL;
  }


  hdr.destID = l_get_u8(packet+RBF_MAC_HEADER_INDEX_DESTID);//packet+12);
  hdr.routID = l_get_u8(packet+RBF_MAC_HEADER_INDEX_ROUTID);//packet+13
  hdr.cmd = l_get_u8(packet+RBF_MAC_HEADER_INDEX_CMD);//packet+14);

  l_put_u8(hdr.destID,ad);
  l_put_u8(hdr.routID,ad+1);
  l_put_u8(hdr.cmd,ad+2);

  return RBF_RET_STATUS_SUCCESS;
}




/*
 *@brief header 组包
 *@param hdr  header结构体数据
 *@param packet  header组包后的数据buffer(含CMD),大端模式
 *@param
 *@return 无返回
 */
void RBF_packet_build_header(RBFMacHeader_t hdr,uint8_t * packet)
{
  l_put_be32(hdr.rollungCode,packet+RBF_MAC_HEADER_INDEX_ROLLUNGCODE);    //4Bytes
  l_put_u8(hdr.frameCtl,packet+RBF_MAC_HEADER_INDEX_FRAMECTL);            //1Byte


  l_put_be16(hdr.syncClock,packet+RBF_MAC_HEADER_INDEX_SYNCCLOCK);        //2Bytes
  l_put_u8(hdr.hsclock,packet+RBF_MAC_HEADER_INDEX_HSCLOCK);              //1Byte

  //hdr.panId(3B) + hdr.srcId(1B)
  l_put_be32(((uint32_t)hdr.panId<<8)|hdr.srcId, packet+RBF_MAC_HEADER_INDEX_PANDID );     //4Byte
  l_put_u8(hdr.destID,packet+RBF_MAC_HEADER_INDEX_DESTID);               //1Byte
  l_put_u8(hdr.routID,packet+RBF_MAC_HEADER_INDEX_ROUTID);              //1Byte
  l_put_u8(hdr.cmd,packet+RBF_MAC_HEADER_INDEX_CMD);                    //1Byte

#if defined (CONFIG_LOG_RBFCOMMON)
  RBF_header_log(hdr);
#endif

}
/*
 *@brief header 解包
 *@param packet  所要解包的数据指针，该数据包含MAC的长度字段
 *@param len     所要解的packet的数据长度
 *@param hdr     解包后header结构体数据
 *@param
 *@return = RBF_RET_STATUS_SUCCESS, 解包成功; =RBF_RET_STATUS_FAIL,解包失败，长度不狗。
 */
RBF_RET_STATUS RBF_header_parse(uint8_t *packet,uint16_t packet_len, RBFMacHeader_t *hdr)
{
  if(packet_len < (RBF_MAC_PACKET_LEN_SIZE+RBF_HEADER_LENGTH_MAX) ){

      return RBF_RET_STATUS_FAIL;
  }

  hdr->rollungCode = l_get_be32(packet+RBF_MAC_HEADER_INDEX_ROLLUNGCODE); //0
  hdr->frameCtl = l_get_u8(packet+RBF_MAC_HEADER_INDEX_FRAMECTL);//4
  hdr->syncClock = l_get_be16(packet+RBF_MAC_HEADER_INDEX_SYNCCLOCK);//5
  hdr->hsclock = l_get_u8(packet+RBF_MAC_HEADER_INDEX_HSCLOCK);//7

  hdr->panId = (l_get_be32(packet+RBF_MAC_HEADER_INDEX_PANDID)>>8)&0x00FFFFFF; //8
  hdr->srcId = l_get_u8(packet+RBF_MAC_HEADER_INDEX_SRCID); //11
  hdr->destID = l_get_u8(packet+RBF_MAC_HEADER_INDEX_DESTID); //12
  hdr->routID = l_get_u8(packet+RBF_MAC_HEADER_INDEX_ROUTID);//13
  hdr->cmd = l_get_u8(packet+RBF_MAC_HEADER_INDEX_CMD); //14



#if defined (CONFIG_LOG_RBFCOMMON)
  RBF_header_log(*hdr);
#endif

  return RBF_RET_STATUS_SUCCESS;
}

void RBF_payload_crypto_key_init (void)
{

}

/*
 *
 *
 *@brief payload AES CCM DE
 *@param
 *@param
 *@param
 *@param
 *@param
 *
 *@param
 *@param  packet 解密后的数据
 *@param  packet_len 解密后数据的长度
 *@return = 0, 数据合法 ; 非0,数据非法
 */
#if 0
void RBF_payload_crypto_packet_parse(const uint8_t *nonce,
                                    /* size_t nonce_length,*/
                                     const uint8_t *additional_data,
                                    /* size_t additional_data_length,*/
                                     uint8_t *ciphertext,
                                     size_t ciphertext_size,
                                     uint8_t *plaintext,
                                     size_t plaintext_length,
                                     size_t *ciphertext_length )

{

}
#endif
/*
 *@brief  payload  AES CCM AE
 *@param
 *@param cipher_payload 含加密后的密文和4Bytes MAC
 *@param cipher_payload_length 为payload+MAC的总长度
 */
#if 0
void RBF_payload_crypto_packet_build (const uint8_t *nonce,
                                      size_t nonce_length,
                                      const uint8_t *additional_data,
                                      size_t additional_data_length,
                                      const uint8_t *plaintext,
                                      size_t plaintext_length,
                                      uint8_t *cipher_payload,
                                      size_t ciphertext_size,
                                      size_t *cipher_payload_length )
{


}
#endif
